#!/usr/bin/env python3

import requests
import json
from urllib.parse import urlencode
import pymongo
from multiprocessing import Pool
import os
import time
# from get_proxy import *

#配置mongodb的信息
from program.paper_program.get_field_name.config import *

client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_Field_NAME_DB]


def conn_microc_academic(name_id,sec_name_id,thr_name_id,ori_name_id):
    #url = 'https://academic.microsoft.com/api/analytics/topics/hierarchy?topicPath=' + str(name_id)
    #url = 'https://academic.microsoft.com/api/analytics/topics/hierarchy?topicPath=71924100'

    #url = 'https://academic.microsoft.com/api/analytics/topics/hierarchy?topicPath='+ str(ori_name_id) + '%2c' + str(name_id)
    #url = 'https://academic.microsoft.com/api/analytics/topics/hierarchy?topicPath=71924100%2C126322002'


    #db['field_name_four'].save({'name':field_data_item.get('name'),'name_id':field_data_item.get('id'),'thr_name':data_item.get('name'),'thr_name_id':data_item.get('name_id'),'sec_name':data_item.get('sec_name'),'sec_name_id':data_item.get('sec_name_id'),'ori_name':data_item.get('ori_name'),'ori_name_id':data_item.get('ori_name_id')})
    #conn_microc_academic(data_item.get('name_id'),data_item.get('sec_name_id'),data_item.get('ori_name_id'))
    # PROXY_POOL_URL = 'http://localhost:5555/random'
    # get_proxy_data = get_proxy(PROXY_POOL_URL)
    # proxy = get_proxy_data.get_proxy_data()
    # proxies = {
    #     'http': "http://" + proxy
    # }


    url = 'https://academic.microsoft.com/api/analytics/topics/hierarchy?topicPath=' + str(ori_name_id) + '%2C'+ str(sec_name_id) + '%2C'+ str(thr_name_id) + '%2C' + str(name_id)
    # url = 'https://academic.microsoft.com/api/analytics/topics/hierarchy?topicPath=71924100%2C126322002%2C170493617'
    #print("11111111111111111111111111111111111111111")
    try:
        # response = requests.get(url,proxies=proxies)
        response = requests.get(url)
        data_json = json.loads(response.content)
        # print(data_json[))
        if response.status_code == 200:
            print("返回的状态码为:~~~~~~~~~",response.status_code)
        #    print(data_json)
            print("返回:",data_json.get('ct'))
            return data_json.get('ct'),response.status_code
        else:
            print("返回的状态码不为200为:",response.status_code)

            return [],response.status_code

    except requests.ConnectionError as e:
        print('出错',e.args)
        return [],405


def get_field_name_one():
    #70000
    field_name_one_data = db['field_name_four'].find({},no_cursor_timeout = True).skip(320000).limit(10000)
    print(len(list(field_name_one_data)))
    # return []
    return field_name_one_data

def do_conn(field_name_one_data):
    field_name_fail_list = []
    for data_item in field_name_one_data:
        #print(data_item.get('name_id'))
        print("data_item:",data_item)
        micro_academic_field_data,status_code = conn_microc_academic(data_item.get('name_id'),data_item.get('sec_name_id'),data_item.get('thr_name_id'),data_item.get('ori_name_id'))
        if status_code == 200 and micro_academic_field_data:
            print(11111111111111111)
            for field_data_item in micro_academic_field_data:

                db['field_name_five_two'].save({'name':field_data_item.get('n'),'name_id':field_data_item.get('id'),'four_name':data_item.get('name'),'four_name_id':data_item.get('name_id'),'thr_name':data_item.get('thr_name'),'thr_name_id':data_item.get('thr_name_id'),'sec_name':data_item.get('sec_name'),'sec_name_id':data_item.get('sec_name_id'),'ori_name':data_item.get('ori_name'),'ori_name_id':data_item.get('ori_name_id')})
    #pool = Pool()
        elif status_code == 405:
            print(2222222222222222)
            field_name_fail_list.append(data_item)
        else:
            print(3333333333333333)
            #field_name_fail_list.append(data_item)
            pass

    return field_name_fail_list



def main():
    field_name_one_data = get_field_name_one()
    # print(len(list(field_name_one_data)))

    # field_name_fail_list = do_conn(field_name_one_data)
    # field_list = []
    # field_name_fail_list = []


    field_name_fail_list = do_conn(field_name_one_data)
    print("最后失败的长度为：",len(field_name_fail_list))
    while len(field_name_fail_list) != 0:
        print(len(field_name_fail_list))
        field_name_fail_list = do_conn(field_name_fail_list)
    print("最后失败的长度为：",len(field_name_fail_list))


if __name__ == '__main__':
    # conn_microc_academic(2778824557,144027150,168741863,86803240)

    main()
